package com.example.qrcode.controller;

import com.example.qrcode.service.GraphicsLibraryService;
import com.example.qrcode.service.ProductDetailService;
import com.example.qrcode.entity.ProductDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/graphics")
public class GraphicsLibraryController {

    @Autowired
    private GraphicsLibraryService graphicsLibraryService;
    
    @Autowired
    private ProductDetailService productDetailService;

    @GetMapping("/svg/{sno}")
    public ResponseEntity<String> getSvgByProductSNo(@PathVariable String sno) {
        System.out.println("=== 主接线图SVG加载 ===");
        System.out.println("产品序列号: " + sno);
        
        try {
            // 1. 根据产品序列号查询产品详情
            ProductDetail productDetail = productDetailService.getByProductSNo(sno);
            
            if (productDetail == null) {
                System.out.println("未找到产品详情，使用默认8出线M图形");
                String defaultSvg = graphicsLibraryService.getSvgByLineNum(8);
                if (defaultSvg == null || defaultSvg.isEmpty()) {
                    return ResponseEntity.notFound().build();
                }
                System.out.println("返回默认8出线M图形（Base64编码）");
                return ResponseEntity.ok(defaultSvg);
            }
            
            // 2. 获取出线数量
            Integer lineNum = productDetail.getLineNum();
            System.out.println("产品出线数量: " + lineNum);
            
            // 3. 根据出线数量获取对应的SVG
            String svg = graphicsLibraryService.getSvgByLineNum(lineNum);
            
            if (svg == null || svg.isEmpty()) {
                System.out.println("未找到对应的SVG图形");
                return ResponseEntity.notFound().build();
            }
            
            // 4. 获取变压器名称（用于前端替换）
            String transformerName = productDetail.getTransformerName();
            if (transformerName == null || transformerName.trim().isEmpty()) {
                transformerName = "配电变压器"; // 默认名称
            }
            
            System.out.println("变压器名称: " + transformerName);
            System.out.println("成功加载SVG图形，出线数量: " + lineNum + "，变压器名称: " + transformerName);
            System.out.println("返回Base64编码的SVG，前端将进行文本替换");
            
            // 直接返回Base64编码的SVG，让前端处理文本替换
            return ResponseEntity.ok(svg);
            
        } catch (Exception e) {
            System.err.println("获取SVG图形时发生异常: " + e.getMessage());
            e.printStackTrace();
            
            // 发生异常时，尝试加载默认的8出线M图形
            try {
                String defaultSvg = graphicsLibraryService.getSvgByLineNum(8);
                if (defaultSvg != null && !defaultSvg.isEmpty()) {
                    System.out.println("异常情况下使用默认8出线M图形");
                    return ResponseEntity.ok(defaultSvg);
                }
            } catch (Exception defaultException) {
                System.err.println("加载默认图形也失败: " + defaultException.getMessage());
            }
            
            return ResponseEntity.internalServerError().body("获取SVG图形失败");
        }
    }
}
